<template>
  <div class="app-container">
    <el-card>
      <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="68px">
        <el-form-item label="指标名称" prop="metricName">
          <el-input
            v-model="queryParams.metricName"
            placeholder="请输入指标名称"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card class="mt20">

      <el-row :gutter="10" class="mb8" type="flex" justify="space-between">
        <el-col :span="6"><span style="font-size: large">k指标设置</span></el-col>
        <el-col :span="1.5">
          <el-button
            v-show="false"
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['wms:salarySetup:add']"
          >新增
          </el-button>
          <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['wms:salarySetup:export']"
          >导出
          </el-button>
        </el-col>
      </el-row>

      <el-table v-loading="loading" :data="salarySetupList" border class="mt20">
        <el-table-column label="指标名称" prop="metricName"/>
        <el-table-column label="权重" prop="weight"/>
        <el-table-column label="平衡点" prop="balancePoint"/>
        <el-table-column label="奖励刻度" prop="rewardScale"/>
        <el-table-column label="奖励金额" prop="reward"/>
        <el-table-column label="处罚刻度" prop="punishmentScale"/>
        <el-table-column label="处罚金额" prop="punishment"/>
        <el-table-column label="备注" prop="remark"/>
        <el-table-column label="操作" align="right" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
                       v-hasPermi="['wms:salarySetup:edit']">修改
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-row>
        <pagination
          v-show="total>0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
        />
      </el-row>

    </el-card>
    <!-- 添加或修改k指标设置对话框 -->
    <el-drawer :title="title" v-model="open" size="50%" append-to-body>
      <el-form ref="salarySetupRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="指标名称" prop="metricName">
          <el-input v-model="form.metricName" placeholder="请输入指标名称"/>
        </el-form-item>
        <el-form-item label="权重" prop="weight">
          <el-input v-model="form.weight" placeholder="请输入权重"/>
        </el-form-item>
        <el-form-item label="平衡点" prop="balancePoint">
          <el-input v-model="form.balancePoint" placeholder="请输入平衡点"/>
        </el-form-item>
        <el-form-item label="奖励刻度" prop="rewardScale">
          <el-input v-model="form.rewardScale" placeholder="请输入奖励刻度"/>
        </el-form-item>
        <el-form-item label="奖励金额" prop="reward">
          <el-input v-model="form.reward" placeholder="请输入奖励金额"/>
        </el-form-item>
        <el-form-item label="处罚刻度" prop="punishmentScale">
          <el-input v-model="form.punishmentScale" placeholder="请输入处罚刻度"/>
        </el-form-item>
        <el-form-item label="处罚金额" prop="punishment">
          <el-input v-model="form.punishment" placeholder="请输入处罚金额"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-drawer>
  </div>
</template>

<script setup name="SalarySetup">
import {
  listSalarySetup,
  getSalarySetup,
  delSalarySetup,
  addSalarySetup,
  updateSalarySetup
} from "@/api/wms/salarySetup";

const {proxy} = getCurrentInstance();

const salarySetupList = ref([]);
const open = ref(false);
const buttonLoading = ref(false);
const loading = ref(true);
const ids = ref([]);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    metricName: undefined,
    weight: undefined,
    balancePoint: undefined,
    rewardScale: undefined,
    reward: undefined,
    punishmentScale: undefined,
    punishment: undefined,
  },
  rules: {
    id: [
      {required: true, message: "id不能为空", trigger: "blur"}
    ],
    metricName: [
      {required: true, message: "指标名称不能为空", trigger: "blur"}
    ],
    weight: [
      {required: true, message: "权重不能为空", trigger: "blur"}
    ],
    balancePoint: [
      {required: true, message: "平衡点不能为空", trigger: "blur"}
    ],
    rewardScale: [
      {required: true, message: "奖励刻度不能为空", trigger: "blur"}
    ],
    reward: [
      {required: true, message: "奖励金额不能为空", trigger: "blur"}
    ],
    punishmentScale: [
      {required: true, message: "处罚刻度不能为空", trigger: "blur"}
    ],
    punishment: [
      {required: true, message: "处罚金额不能为空", trigger: "blur"}
    ],
  }
});

const {queryParams, form, rules} = toRefs(data);

/** 查询k指标设置列表 */
function getList() {
  loading.value = true;
  listSalarySetup(queryParams.value).then(response => {
    salarySetupList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    metricName: null,
    weight: null,
    balancePoint: null,
    rewardScale: null,
    reward: null,
    punishmentScale: null,
    punishment: null,
    createTime: null,
    updateTime: null,
    createBy: null,
    updateBy: null,
    remark: null
  };
  proxy.resetForm("salarySetupRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加k指标设置";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getSalarySetup(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改k指标设置";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["salarySetupRef"].validate(valid => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id != null) {
        updateSalarySetup(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        }).finally(() => {
          buttonLoading.value = false;
        });
      } else {
        addSalarySetup(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        }).finally(() => {
          buttonLoading.value = false;
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除k指标设置编号为"' + _ids + '"的数据项？').then(function () {
    loading.value = true;
    return delSalarySetup(_ids);
  }).then(() => {
    loading.value = true;
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  }).finally(() => {
    loading.value = false;
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('wms/salarySetup/export', {
    ...queryParams.value
  }, `salarySetup_${new Date().getTime()}.xlsx`)
}

getList();
</script>
